package ntd.util.modular.data.util.handle.context;

import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import ntd.util.modular.data.entity.DataAcquisitionRule;
import ntd.util.config.common.enums.data.EnumProtocolType;

@Data
public class ContextDataRuleExecute {

    private DataAcquisitionRule dataAcquisitionRule;
    private EnumProtocolType enumProtocolType;
    private String cron;
    private String host;
    private String port;
    private String path;
    private String params;
    private String logicParams;
    private String headers;
    private String dataType;
    private Method method;

    public static ContextDataRuleExecute init(DataAcquisitionRule dataAcquisitionRule) {
        ContextDataRuleExecute contextDataRuleExecute = new ContextDataRuleExecute();
        contextDataRuleExecute.setDataAcquisitionRule(dataAcquisitionRule);

        JSONObject jsonObject = JSONUtil.parseObj(dataAcquisitionRule.getExtendJson());
        contextDataRuleExecute.setEnumProtocolType(dataAcquisitionRule.getEnumProtocolType());
        contextDataRuleExecute.setCron(jsonObject.getStr("httpCron"));
        contextDataRuleExecute.setHost(jsonObject.getStr("httpHost"));
        contextDataRuleExecute.setPort(jsonObject.getStr("httpPort"));
        contextDataRuleExecute.setPath(jsonObject.getStr("httpPath"));
        contextDataRuleExecute.setParams(jsonObject.getStr("httpParams"));
        contextDataRuleExecute.setLogicParams(jsonObject.getStr("httpLogicParams"));
        contextDataRuleExecute.setHeaders(jsonObject.getStr("httpHeaders"));
        contextDataRuleExecute.setDataType(jsonObject.getStr("httpDataType"));
        contextDataRuleExecute.setMethod(Method.valueOf(jsonObject.getStr("httpType").toUpperCase()));
        return contextDataRuleExecute;
    }
}